//
//  ZWBWebVC.m
//  Cat
//
//  Created by dllo on 15/11/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZWBWebVC.h"
#import "ReadTableViewCell.h"
#import "ZWBOneImageCell.h"
#import "ZWBdetailModel.h"
#import "TimeTool.h"
@interface ZWBWebVC ()<UIWebViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *userArr;
@property(nonatomic, strong)NSDictionary *dic;
@property(nonatomic, strong)NSMutableArray *firstArr;
@end

@implementation ZWBWebVC

{
    UIActivityIndicatorView *_acView;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    if (![self.model.ftype isEqualToString:@"album"] && ![self.model.ftype isEqualToString:@"pic"]) {
        [self createWeb];
    } else {
        [self createTableView];
        [self createData];
    }
}

- (void)createWeb {
    
    UIWebView *wb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT + 49)];
    [self.view addSubview:wb];
    wb.delegate = self;
    wb.scalesPageToFit = YES;
    _acView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32.0f, 32.0f)];
    // 设置样式
    _acView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    _acView.center = self.view.center;
    [self.view addSubview:_acView];
    NSURL *url = [NSURL URLWithString:self.model.webUrl];
    NSURLRequest *request =  [NSURLRequest requestWithURL:url];
    [wb loadRequest:request];
}


- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) style:UITableViewStylePlain];
    
    [self.view addSubview:_tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[ReadTableViewCell class] forCellReuseIdentifier:@"readReuse"];
    [self.tableView registerClass:[ZWBOneImageCell class] forCellReuseIdentifier:@"oneImageReuse"];
}

- (void)createData {
    NSString *ssss  = nil;
    if ([self.model.ftype isEqualToString:@"album"]) {
   
        ssss = [NSString stringWithFormat:@"http://api.huapipi.com/recommend/v2/detail/%@?uid=1033275", self.model.fid];
    } else if ([self.model.ftype isEqualToString:@"pic"]) {
        ssss = [NSString  stringWithFormat:@"http://api.huapipi.com/dynamic/l1/detail/%@?deviceId=1826162&uid=1033268&w=720&channelId=0&h=1280", self.model.fid];
    }
    
    [ToolNetWorking ToolNetWorking:ssss block:^(id result) {
        self.dic = [NSMutableDictionary dictionary];
        self.dic = result[@"dynamic"];
        ZWBdetailModel *model = [[ZWBdetailModel alloc] init];
        [model setValuesForKeysWithDictionary:self.dic];

        self.userArr = [NSMutableArray arrayWithArray:result[@"users"]];
        
        self.firstArr = [ZWBdetailModel BaseModel:result[@"rows"]];
        
        [self.tableView reloadData];
    }];
    
    
}

#pragma mark tableDatasource 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.model.ftype isEqualToString:@"pic"]) {
        return 1;
    }else if ([self.model.ftype isEqualToString:@"album"]) {
        return self.firstArr.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.model.ftype isEqualToString:@"album"]) {
        if ([[self.firstArr[indexPath.row] ftype] isEqualToString:@"talk"] ) {
            ReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"readReuse"];
            //        cell.nameLabel.text = self.dic;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            ZWBdetailModel *model = self.firstArr[indexPath.row];
            
            cell.model = (HotModel *)model;
             cell.wordLabel.text = model.content[@"word"];
            cell.likeLabel.text = [NSString stringWithFormat:@"%@", model.likes];
            HotModel *memem = self.firstArr[indexPath.row];
            cell.commentsView.HeaderModel = memem;
            cell.timeLabel.text = [TimeTool Whattime:model.content[@"time"]];
            return cell;

        }else if ([[self.firstArr[indexPath.row] ftype] isEqualToString:@"pic"]) {
             ZWBOneImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oneImageReuse"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            ZWBdetailModel *model = self.firstArr[indexPath.row];
            NSArray *array = [model.content [@"url"]  componentsSeparatedByString:@","];
            cell.arr = array;
            cell.likeLabel.text = [NSString stringWithFormat:@"%@", model.likes];
            cell.commentsLabel.text = [NSString stringWithFormat:@"%@", model.comments];
            cell.wordLabel.text = model.content[@"word"];
            cell.timeLabel.text = [TimeTool Whattime:model.content[@"time"]];
            HotModel *memem = self.firstArr[indexPath.row];
            cell.commentsView.HeaderModel = memem;

            cell.model = (HotModel *)model;

            return cell;
        }

        

        
        
    } else if ([self.model.ftype isEqualToString:@"pic"]) {
        
        
         ZWBOneImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oneImageReuse"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSArray *array = [self.dic[@"content"][@"url"] componentsSeparatedByString:@","];
           
            cell.arr = array;
            cell.dic = self.dic;
            [cell.headerImageView sd_setImageWithURL:[NSURL URLWithString:self.userArr[indexPath.row][@"portrait"]]];
            cell.nameLabel.text = [self.userArr lastObject] [@"nickname"];

        
        return cell;
        
    }
    
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.model.ftype isEqualToString:@"pic"] ) {
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
        NSString *str = self.dic[@"content"][@"word"];
        CGRect rect = [str boundingRectWithSize:CGSizeMake(VIEW_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        return VIEW_HEIGHT/2 + rect.size.height;
    } else if ([self.model.ftype isEqualToString:@"album"]) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
        NSString *str = [self.firstArr[indexPath.row] content][@"word"];
        CGRect rect = [str boundingRectWithSize:CGSizeMake(VIEW_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

        if ([[self.firstArr[indexPath.row] ftype] isEqualToString:@"pic"]) {
            
            return VIEW_HEIGHT/2 + rect.size.height;
            
        }else if ([[self.firstArr[indexPath.row] ftype] isEqualToString:@"talk"]) {
            
            
            return 200 + rect.size.height ;
        }
    }
    return 10;
}




#pragma mark webDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    [_acView startAnimating];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    [_acView stopAnimating];
    [_acView removeFromSuperview];
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
    
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:@"" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *OKAC = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
//    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
//    [alert addAction:OKAC];
//    [alert addAction:cancel];
//    [self presentViewController:alert animated:YES completion:nil];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
